define(function(require, exports){
	var $ = require("jquery");
	var pubjs = require('pubjs');
	var view  = require('@base/view');
	require('@tpl');
	var util = require('util');

	// 选项卡-外部链接
	var MaterialLink = view.container.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'class': 'uk-form P-whiskyFormTab',
				'tplFile': '/tpl/whisky/outerLink.html',
				'view_model': {
					'show': {
						deepLink: pubjs.checkRight('whisky.deepLink')
					}
				}
			});

			this.Super('init', arguments);
		},
		afterBuild: function(){
			var self = this;
			var el = this.getDOM();
			var config = {
				input: {
					"placeholder":LANG("请输入外部链接地址，以http://或者https://开头")
				}
				,LandingPage: {
					width: 500,
					"placeholder": LANG("最终落地页地址，并不需要填，系统自动获取。如勾了强制更新就可以手动填")
				}
				,LandingPageTip: {
					'width': 300,
					'data': LANG('当落地页有做302跳转时，渠道需要审核最终着陆页')
				}
				,LandingPageUpdate: {
					'class': 'di',
					data: [{text: LANG('强制更新'),
							value: 'LandingPageUpdate',
							checked: false}],
					changeEvent: true,
					labelWidth: 120
				}
				,DeepLink: {
					width: 500
				}
				,DeepLinkTip: {
					'width': 300,
					'data': LANG('DeepLink指直接打开App内某页面，需要广告主和媒体同时支持该技术，当以上条件未能满足或该App未安装，将直接跳转以上填写的落地页。对应DeepLink链接格式请咨询App开发人员')
				}
			};

			self.buildFromTemplate(config);
			self.uiBind(el.find('.landingPageUpdate input'), 'click', self.onUpdateChecked);
			// 链接去代码中心
			this.dg(el, "#codeCenter", "click", "eventGotoCodeCenter");
		},
		afterBuildTemplate: function() {
			this.$.LandingPage.disable();
			this.$.LandingPage.reset();
		},
		eventGotoCodeCenter: function(evt, elm){
			window.open('#admin/codeCenter');
			return false;
		},
		getData: function(){
			var protocolFound = function(url) {
				var regex = "^https?:\/\/";
				var found = url.match(regex);
				return found !== null;
			};
			var ret = {};
			var value = this.$.input.getValue();
			value = util.trim(value);
			if(!value)
			{
				ret.link = '';
			}
			else
			{
				// 若没加'http://'或'https://',自动补上'http://'
				var isMatch = protocolFound(value);
				ret.link = !isMatch ? ('http://' + value) : value;
			}

			ret.DeepLink = this.$.DeepLink.getValue();

			var updateChecked = this.$.LandingPageUpdate.getData('complete');
			if(updateChecked && updateChecked.length)
			{
				ret.LandingPageUpdate = true;
				var landingPage = this.$.LandingPage.getValue().trim();
				if(landingPage && !protocolFound(landingPage))
				{
					landingPage = 'http://' + landingPage;
				}
				ret.LandingPage = landingPage;
			}
			else
			{
				ret.LandingPageUpdate = false;
			}

			return ret;
		},
		setData: function(data){
			this.$.input.setValue(data.Material);
			this.$.LandingPage.setValue(data.LandingPage || '');
			this.$.DeepLink.setValue(data.DeepLink || '');
			this.$.LandingPageUpdate.setData(data.manualLandPage ? ['LandingPageUpdate'] : []);
		},
		reset: function(){
			if(this.$){
				this.$.input.setValue('');
				this.$.LandingPage.setValue('');
				this.$.DeepLink.setValue('');
				this.$.LandingPageUpdate.reset();
			}
		},
		onUpdateChecked: function () {
			var updateChecked = this.$.LandingPageUpdate.getData();
			if(updateChecked.indexOf('LandingPageUpdate') > -1) {
				this.$.LandingPage.enable();
			} else {
				this.$.LandingPage.disable();
			}
		}
	});
	exports.materialLink = MaterialLink;

	// 选项卡-本地上传
	var MaterialUpload = view.container.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'class': 'P-whiskyMaterial P-whiskyFormTab',
				'whiskyName': ''
			});

			this.Super('init', arguments);
		},
		afterBuild: function(){
			this.append([
				'<div class="P-whiskyMaterialTitle">'+LANG('请选择一个落地页模版：')+'</div>',
				'<div pub-name="radioGroup" pub-mod="modules/loadOnVisible.base" class="ml20"/>',
				// '<div pub-name="radioGroup" pub-mod="@base/common/input.radioGroup"/>',
				'<div pub-name="uploader" pub-mod="modules/upload.base" />'
			].join(''));

			var param = $.extend({}, pubjs.config('uploader/whisky'), {
				target: this.getDOM(),
				subModule: 'whisky/uploader.base',
				hasProgress: false
			});

			var config = {
				'radioGroup':{
					'layout':{
						module: '@base/common/input.radio',
						url: 'layout/option',
						reqType: 'websocket',
						key: 'id',
						name: 'name',
						labelWidth: '16%',
						autoLoad: false,
						labelHeight: 20
					}
				},
				'uploader': param
			};

			this.buildFromTemplate(config);
		},
		afterBuildTemplate: function() {
			this.cast('startLoadData');
		},
		getData: function(){
			return {
				'Id': this.$.uploader.getValue(),
				'Layout': +this.$.radioGroup.getData()
			};
		},
		reset: function(){
			this.$.uploader.reset();
			this.$.radioGroup.reset();
		},
		setData: function(data){
			if ( data.MaterialDetails ) {
				this.$.uploader.setValue(data.MaterialDetails);
			}
			this.$.radioGroup.setData(data.Layout);
		}
	});
	exports.materialUpload = MaterialUpload;


	var MaterialCreative = view.container.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'class': 'uk-form',
				'whiskyName': ''
			});

			this.Super('init', arguments);
		},
		afterBuild: function(){

			var config = pubjs.config('uploader/creative');

			var param = $.extend({}, config, {
				target: this.getDOM(),
				subModule: 'modules/upload.creative'
			});

			this.createAsync('uploader', 'modules/upload.base', param);
		},
		getData: function(){
			return this.$.uploader.getValue();
		},
		reset: function(){
			this.$.uploader.reset();
		},
		setData: function(data){
			this.$.uploader.setValue(data);
		}
	});
	exports.materialCreative = MaterialCreative;

});